package org.celery.client.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.celery.client.vo.WebInfoVo;
import org.celery.modules.other.entity.Pixel;
import org.celery.modules.other.entity.SevenElevenShop;
import org.celery.modules.other.enums.PixelStatusEnum;
import org.celery.modules.other.service.IPixelService;
import org.celery.modules.other.service.ISevenElevenShopService;
import org.celery.modules.other.vo.SevenElevenShopVO;
import org.celery.modules.other.wrapper.SevenElevenShopWrapper;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.system.entity.Param;
import org.springblade.modules.system.service.IParamService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("misc")
@Api(value = "杂项")
public class ClientMiscController {

    private final IParamService paramService;
    private final IPixelService pixelService;
    private final ISevenElevenShopService sevenElevenShopService;

    /**
     * 网站信息
     */
    @GetMapping("/web-info")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "网站信息")
    public R<WebInfoVo> detail(String domain) {
        Param fbMsgIdParam = paramService.getOne(Wrappers.<Param>lambdaQuery()
                .eq(Param::getParamKey, "fb_msg_id")
        );
        WebInfoVo webInfo = new WebInfoVo();
        if (Func.isNotEmpty(fbMsgIdParam)) {
            webInfo.setFbMsgId(fbMsgIdParam.getParamValue());
        }

        List<Pixel> pixelList = pixelService.list(Wrappers.<Pixel>lambdaQuery()
                .eq(Pixel::getDomain, domain)
                .eq(Pixel::getStatus, PixelStatusEnum.NORMAL.getStatus())
        );
        webInfo.setPixelList(pixelList);

        return R.data(webInfo);
    }

    /**
     *  7-11商超列表
     */
    @GetMapping("/seven-eleven-list")
    @ApiOperationSupport(order = 2)
    public R<List<SevenElevenShopVO>> sevenElevenList(SevenElevenShop sevenElevenShop) {
        QueryWrapper<SevenElevenShop> queryWrapper = Condition.getQueryWrapper(sevenElevenShop);
        queryWrapper.lambda().orderByAsc(SevenElevenShop::getSerial);
        if (Func.isNotEmpty(sevenElevenShop.getAddress())) {
            queryWrapper.lambda().like(SevenElevenShop::getAddress, sevenElevenShop.getAddress());
            sevenElevenShop.setAddress(null);
        }
        return R.data(SevenElevenShopWrapper.build().listVO(sevenElevenShopService.list(queryWrapper)));
    }

}
